﻿using System;
using System.Data;
using System.Text.RegularExpressions;
using TSL.BLL;

namespace TSL.Web.UI
{
    public class BuyBetSSC
    {
        public static bool BuyCheckTotalAmount(string allmoney, string onemoney, string multiple, string sscid)
        {
            int Amount = 0;
            Amount = Convert.ToInt32((double)(Convert.ToDouble(allmoney) / (Convert.ToDouble(onemoney) * Convert.ToDouble(multiple))));
            DataSet ds = new BetMenu().GetListFromView("id=" + sscid);
            int cunt = ds.Tables[0].Rows.Count;
            int totalAmount = Convert.ToInt32(ds.Tables[0].Rows[0]["totalAmount"].ToString().Trim());
            if (Amount > totalAmount)
            {
                return false;
            }
            return true;
        }

        public static bool CheckBetMenu(string sscid)
        {
            if (new caipiao().GetListFromView("id in(select caipiao_id from betmenu where id=" + sscid + " and state=100)and status=100").Tables[0].Rows.Count != 1)
            {
                return false;
            }
            return true;
        }

        public static bool CheckCodes(string codes)
        {
            string[] c1 = codes.Split(new char[] { '$' });
            bool flag = true;
            Regex reg = new Regex("^[0-9]+$");
            foreach (string c1_1 in c1)
            {
                string[] c3 = c1_1.Split(new char[] { '|' })[0].ToString().Trim().Split(new char[] { ',' });
                foreach (string c3_1 in c3)
                {
                    if (c3_1.IndexOf(" ") >= 0)
                    {
                        string[] c4 = c3_1.Split(new char[] { ' ' });
                        foreach (string c4_1 in c4)
                        {
                            if (!reg.Match(c4_1).Success)
                            {
                                flag = false;
                            }
                        }
                    }
                    else if ((c3_1 != "") && !reg.Match(c3_1).Success)
                    {
                        flag = false;
                    }
                }
            }
            return flag;
        }

        public static bool CheckIssue(string expect, string sscid)
        {
            bool flag = true;
            DataSet ds = new GetOpenCodes().GetListFromView("ssc_id in(select caipiao_id from betmenu where id=" + sscid + ")");
            int cunt = ds.Tables[0].Rows.Count;
            if (cunt <= 0)
            {
                flag = false;
            }
            else
            {
                string excp = ds.Tables[0].Rows[0]["lottery_num"].ToString().Trim();
                if ((cunt != 1) || (excp != expect))
                {
                    return false;
                }
            }
            return flag;
        }

        public static bool CheckTotalAmount(string codes, string AllMoney, string multiple)
        {
            string[] c1 = codes.Split(new char[] { '$' });
            decimal newmoney = 0M;
            foreach (string c1_1 in c1)
            {
                string c2_1 = c1_1.Split(new char[] { '|' })[1].ToString().Trim();
                newmoney += Convert.ToDecimal(c2_1);
            }
            return ((newmoney * Convert.ToDecimal(multiple)) == Convert.ToDecimal(AllMoney));
        }

        public static string ReCodes(string codes)
        {
            string[] c1 = codes.Split(new char[] { '$' });
            string s = string.Empty;
            foreach (string c1_1 in c1)
            {
                string c2_1 = c1_1.Split(new char[] { '|' })[0].ToString().Trim();
                s = s + "$" + c2_1;
            }
            return s.Substring(1, s.Length - 1);
        }
    }

}
